package managerEngine

import (
	"fmt"
	"strings"
	"time"

	"gitee.com/RocsSun/report/cache"
	"github.com/go-resty/resty/v2"
	"github.com/tidwall/gjson"
)

// var DevicesMonitorResponse *resty.Response

type DailyReportQueryParam struct{}

func (d DailyReportQueryParam) MakeJSTime() string {
	return fmt.Sprint(time.Now().UnixNano() / 1000000)
}

func (d DailyReportQueryParam) CPUQueryParam(ip string) *map[string]string {
	return &map[string]string{
		"name":       ip,
		"policyName": "Win-CPUUtilization",
		"index":      "Win-CPUUtilization",
		"period":     "Yesterday",
		"withMMA":    "true",
		"_":          d.MakeJSTime(),
	}
}

func (d DailyReportQueryParam) DiskQueryParam(ip string) []*map[string]string {
	response := GetCacheDeviceMonitorResponse(ip)
	var disksParms []*map[string]string
	for _, v := range gjson.GetBytes(response.Body(), "performanceMonitors.monitors.#.name").Array() {
		if strings.Contains(v.String(), "Serial") {
			disksParms = append(disksParms, &map[string]string{
				"name":       ip,
				"policyName": "PartitionWiseDiskDetails",
				"index":      v.String(),
				"period":     "Yesterday",
				"withMMA":    "true",
				"_":          d.MakeJSTime(),
			})
		}
	}
	return disksParms
}

func (d DailyReportQueryParam) MemQueryParam(ip string) *map[string]string {
	return &map[string]string{
		"name":       ip,
		"policyName": "Win-MemoryUtilization",
		"index":      "Win-MemoryUtilization",
		"period":     "Yesterday",
		"withMMA":    "true",
		"_":          d.MakeJSTime(),
	}
}

func (d DailyReportQueryParam) ProcessQueryParam(ip string) map[string]*map[string]string {
	response := GetCacheDeviceMonitorResponse(ip)
	var processDailyReportQueryParam map[string]*map[string]string = make(map[string]*map[string]string)

	for _, v := range gjson.GetBytes(response.Body(), "processMonitors.monitors").Array() {
		// fmt.Println(v.Get("name"))
		processName := v.Get("name").String()
		for i := 1; i < 20; i++ {
			// testResult 存在了服务名。添加新的服务名。
			if _, ok := processDailyReportQueryParam[processName]; ok {
				processName = processName + fmt.Sprint(i)
			} else {
				break
			}
		}
		processDailyReportQueryParam[processName] = &map[string]string{
			"ID":          v.Get("id").String(),
			"TimePeriod":  "Yesterday",
			"isFluidic":   "true",
			"graphFormat": "true",
			"_":           d.MakeJSTime(),
		}
	}
	return processDailyReportQueryParam
}

func (d DailyReportQueryParam) ServiceQueryParam(ip string) []*map[string]string {

	response := GetCacheDeviceMonitorResponse(ip)

	var serviceDailyReportQueryParam []*map[string]string
	for _, v := range gjson.GetBytes(response.Body(), "serverMonitors.monitors").Array() {
		serviceDailyReportQueryParam = append(serviceDailyReportQueryParam, &map[string]string{
			"Name":       ip,
			"Instance":   v.Get("name").String(),
			"TimePeriod": "Today",
			"isFluidic":  "true",
			"elementID":  v.Get("moid").String(),
			"_":          d.MakeJSTime(),
		})
	}
	return serviceDailyReportQueryParam
}

func (d DailyReportQueryParam) DeviceSummaryQueryParam(ip string) *map[string]string {
	return &map[string]string{
		"name":      ip,
		"isFluidic": "true",
		"snapType":  "MonitoringDevice",
		"_":         d.MakeJSTime(),
	}
}

func GetDeviceMonitorResponse(ip string) *resty.Response {
	return NewOpManagerClient().DeviceMonitors(ip)
}

func GetCacheDeviceMonitorResponse(ip string) *resty.Response {
	if response, ok := cache.Cache.Get(ip); ok {
		return response.(*resty.Response)
	} else {
		response := GetDeviceMonitorResponse(ip)
		cache.Cache.SetDefault(ip, response)
		return response
	}
}
